package com.sxt.system.service.impl;

import com.sxt.system.domain.Dept;
import com.sxt.system.mapper.DeptMapper;
import com.sxt.system.service.IDeptService;
import com.sxt.system.utils.DataGridView;
import com.sxt.system.vo.DeptVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.io.Serializable;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 俊哥
 * @since 2019-11-11
 */
@Service
public class DeptServiceImpl extends ServiceImpl<DeptMapper, Dept> implements IDeptService {

	@Override
	public DataGridView loadAllDept(DeptVO deptVO) {
		DeptMapper mapper = this.getBaseMapper();
		Page<Dept> page = new Page<>(deptVO.getPage(),deptVO.getLimit());
		QueryWrapper<Dept> qw = new QueryWrapper<Dept>();
		qw.like(StringUtils.isNoneBlank(deptVO.getTitle()), "title", deptVO.getTitle());
		qw.like(StringUtils.isNoneBlank(deptVO.getAddress()), "address", deptVO.getAddress());
		qw.like(StringUtils.isNoneBlank(deptVO.getRemark()), "remark", deptVO.getRemark());
		qw.eq(deptVO.getId()!=null, "id", deptVO.getId()).or().eq(deptVO.getId()!=null, "pid", deptVO.getId());
		qw.orderByAsc("ordernum");
		mapper.selectPage(page, qw);
		return new DataGridView(page.getTotal(),page.getRecords());
	}

	@Override
	public List<Dept> queryAllDeptForList(DeptVO deptVO) {
		QueryWrapper<Dept> qw = new QueryWrapper<Dept>();
		qw.eq(deptVO.getAvailable()!=null, "available", deptVO.getAvailable());
		return this.getBaseMapper().selectList(qw);
	}

	@Override
	public Integer queryDeptMaxOrderNum() {
		return this.getBaseMapper().queryDeptMaxOrderNum();
	}

	@Override
	public Dept addDept(Dept dept) {
		this.getBaseMapper().insert(dept);
		return dept;
	}

	@Override
	public Dept updateDept(Dept dept) {
		this.getBaseMapper().updateById(dept);
		return dept;
	}

	@Override
	public Integer queryDeptCountByPid(Integer id) {
		return this.getBaseMapper().queryDeptCountByPid(id);
	}

	@Override
	public Dept getById(Serializable id) {
		return super.getById(id);
	}

	@Override
	public boolean removeById(Serializable id) {
		return super.removeById(id);
	}

	
}
